package com.dstation.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dstation.domain.CollectionInfo;
import com.dstation.domain.MqMessage;
import com.dstation.domain.UserInfo;
import com.dstation.domain.VideoInfo;
import com.dstation.mq.producer.BrowserMqProducer;
import com.dstation.service.interf.ICollectVideoService;
import com.dstation.utils.MQConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.sql.Timestamp;
import java.util.Objects;
import java.util.UUID;

@Slf4j
@Controller
@RequestMapping(value = "/collect")
public class CollectVideoController {

    final int pageSize=10;
    /**
     * @author:  Jeadon
     * @create:  2022/4/7 15:27
     * @tip:   收藏类
     * @param:
     *
     **/
    @Autowired
    private ICollectVideoService collectVideoService;



    //收藏视频
    @ResponseBody
    @RequestMapping(value = "/collectVideo")
    public JSONObject saveCollection(HttpSession httpSession,@PathParam("videoId")Long videoId){
        JSONObject jsonObject=new JSONObject();

        UserInfo userInfo =(UserInfo) httpSession.getAttribute("userInfo");

        //判断是否已收藏
        boolean isHave=collectVideoService.findHaveCollection(userInfo.getUserId(),videoId);
        boolean isSave=false;
        if(isHave){
            isSave=collectVideoService.cancelCollections(userInfo.getUserId(),videoId);
        }else{
            isSave=collectVideoService.memoryCollections(userInfo.getUserId(),videoId);
        }


        jsonObject.put("isSave",isSave);

        return jsonObject;
    }


    //取消收藏
    @ResponseBody
    @RequestMapping(value = "/cancelCollect")
    public JSONObject removeCollectionVideo(HttpSession httpSession,@PathParam("videoId")Long videoId){
        JSONObject jsonObject=new JSONObject();

        UserInfo userInfo =(UserInfo) httpSession.getAttribute("userInfo");

        boolean isCancel=collectVideoService.cancelCollections(userInfo.getUserId(),videoId);

        jsonObject.put("isCancel",true);

        return jsonObject;
    }


    //查看收藏
    @RequestMapping(value = "/viewCollect")
    public String  queryCollections(HttpSession httpSession, @PathParam("page")Integer page, ModelMap modelMap){


        UserInfo userInfo =(UserInfo) httpSession.getAttribute("userInfo");

        if(Objects.isNull(userInfo)||Objects.isNull(page)){
            return "user/loginAndRegister";
        }

        IPage<VideoInfo> list =collectVideoService.findCollections(new Page<>(page,pageSize),userInfo.getUserId());

        modelMap.put("videoPageInfo",list);

        return "main/myCollection";
    }


}
